package com.cw.jeeyt.service.impl.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiDatabaseDocBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiDatabaseDocDao;
import com.cw.jeeyt.dao.model.api.ApiDatabaseDocDo;
import com.cw.jeeyt.service.intf.api.ApiDatabaseDocService;
import com.cw.jeeyt.service.converter.api.ApiDatabaseDocConverter;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 数据库信息业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiDatabaseDocServiceImpl implements ApiDatabaseDocService {

    private static final Logger logger = LoggerFactory.getLogger(ApiDatabaseDocServiceImpl.class);
    @Autowired
    private ApiDatabaseDocDao apiDatabaseDocDao;//数据库信息数据库操作接口

    /**
     * 查询所有数据库信息
     */
    @Override
    public List<ApiDatabaseDocBo> queryApiDatabaseDocList() throws Exception {
        return ApiDatabaseDocConverter.doList2BoList(apiDatabaseDocDao.queryApiDatabaseDocList());
    }

    /**
     * 分页查询数据库信息
     *
     * @param apiDatabaseDocBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiDatabaseDocBo> queryByPage(ApiDatabaseDocBo apiDatabaseDocBo, Integer startNum, Integer pageSize,
        Order order) throws Exception {
        if (apiDatabaseDocBo == null) {
            logger.error("数据库信息为空");
            return new PageInfo<>();
        }
        PageInfo<ApiDatabaseDocDo> pageInfo = apiDatabaseDocDao.queryByPage(
            ApiDatabaseDocConverter.bo2Do(apiDatabaseDocBo), startNum, pageSize, order);
        List<ApiDatabaseDocBo> apiDatabaseDocBos = ApiDatabaseDocConverter.doList2BoList(pageInfo.getList());
        if (CollectionUtil.isNotEmpty(apiDatabaseDocBos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("databaseType", "std_database_type");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(apiDatabaseDocBos)),
                dictMap);
            apiDatabaseDocBos = JSON.parseArray(jsonArray.toJSONString(), ApiDatabaseDocBo.class);
        }
        return new PageInfo<>(pageInfo, apiDatabaseDocBos);
    }

    /**
     * 根据主键查询数据库信息
     *
     * @return
     */
    @Override
    public ApiDatabaseDocBo getApiDatabaseDocById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return ApiDatabaseDocConverter.do2Bo(apiDatabaseDocDao.getById(id));
    }

    /**
     * 新增数据库信息
     *
     * @param apiDatabaseDocBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ApiDatabaseDocBo apiDatabaseDocBo) throws Exception {

        if (apiDatabaseDocBo == null) {
            logger.error("数据库信息为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseDocDo apiDatabaseDoc = ApiDatabaseDocConverter.bo2Do(apiDatabaseDocBo);
        apiDatabaseDoc.setCreateBy(userBo.getId());
        apiDatabaseDoc.setCreateDate(new Date());
        apiDatabaseDoc.setUpdateBy(userBo.getId());
        apiDatabaseDoc.setUpdateDate(new Date());
        Integer result = apiDatabaseDocDao.insert(apiDatabaseDoc);
        return result;
    }

    /**
     * 新增数据库信息不为空的字段
     *
     * @param apiDatabaseDocBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiDatabaseDocBo apiDatabaseDocBo) throws Exception {

        if (apiDatabaseDocBo == null) {
            logger.error("数据库信息为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseDocDo apiDatabaseDoc = ApiDatabaseDocConverter.bo2Do(apiDatabaseDocBo);
        apiDatabaseDoc.setCreateBy(userBo.getId());
        apiDatabaseDoc.setCreateDate(new Date());
        apiDatabaseDoc.setUpdateBy(userBo.getId());
        apiDatabaseDoc.setUpdateDate(new Date());
        Integer result = apiDatabaseDocDao.insertSelective(apiDatabaseDoc);
        return result;
    }

    /**
     * 更新数据库信息
     *
     * @param apiDatabaseDocBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiDatabaseDocBo apiDatabaseDocBo) throws Exception {

        if (apiDatabaseDocBo == null) {
            logger.error("数据库信息为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseDocDo apiDatabaseDoc = ApiDatabaseDocConverter.bo2Do(apiDatabaseDocBo);
        apiDatabaseDoc.setUpdateBy(userBo.getId());
        apiDatabaseDoc.setUpdateDate(new Date());

        apiDatabaseDocDao.update(apiDatabaseDoc);
    }

    /**
     * 更新数据库信息不为空的字段
     *
     * @param apiDatabaseDocBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiDatabaseDocBo apiDatabaseDocBo) throws Exception {

        if (apiDatabaseDocBo == null) {
            logger.error("数据库信息为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseDocDo apiDatabaseDoc = ApiDatabaseDocConverter.bo2Do(apiDatabaseDocBo);
        apiDatabaseDoc.setUpdateBy(userBo.getId());
        apiDatabaseDoc.setUpdateDate(new Date());

        apiDatabaseDocDao.updateSelective(apiDatabaseDoc);
    }

    /**
     * 删除数据库信息
     *
     * @param apiDatabaseDocBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiDatabaseDocBo apiDatabaseDocBo) throws Exception {

        if (apiDatabaseDocBo == null) {
            logger.error("数据库信息为空");
            return;
        }
        ApiDatabaseDocDo apiDatabaseDoc = ApiDatabaseDocConverter.bo2Do(apiDatabaseDocBo);
        apiDatabaseDocDao.delete(apiDatabaseDoc);
    }
}
